<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="项目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="project">
              <a-input v-model="model.project" placeholder="请输入项目"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="生产阶段" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productionPhase">
              <a-input v-model="model.productionPhase" placeholder="请输入生产阶段"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="版本号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="versionNumber">
              <a-input v-model="model.versionNumber" placeholder="请输入版本号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="料号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialNumber">
              
              <j-dict-select-tag
                v-model="model.materialNumber"
                dictCode="material_number"
                placeholder="请输入料号"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="quantity">
              <a-input v-model="model.quantity" placeholder="请输入数量"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="生产日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productionDate">
              <a-input v-model="model.productionDate" placeholder="请输入生产日期（示例：20230323）"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="生产批号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productionBatchNumber">
              <a-input v-model="model.productionBatchNumber" placeholder="请输入生产批号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="料件描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="itemDescription">
              <a-input v-model="model.itemDescription" placeholder="请输入料件描述"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="Config" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="itemConfig">
              <a-input v-model="model.itemConfig" placeholder="请输入Config"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="湿敏度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="humiditySensitivity">
              <a-input v-model="model.humiditySensitivity" placeholder="请输入湿敏度"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="供应商代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supplierCode">
              <a-input :disabled="isDisabled" v-model="model.supplierCode" placeholder="请输入供应商代码"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="供应商名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supplierName">
              <a-input :disabled="isDisabled" v-model="model.supplierName" placeholder="请输入供应商名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="叉板数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="forkPlateQuantity">
              <a-input v-model="model.forkPlateQuantity" placeholder="请输入叉板数量"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="BIN号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="binNumber">
              <a-input v-model="model.binNumber" placeholder="请输入BIN号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="模穴号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cavityNumber">
              <a-input v-model="model.cavityNumber" placeholder="请输入模穴号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="原产地" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="origin">
              <a-input :disabled="isDisabled" v-model="model.origin" placeholder="请输入原产地"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="itemModel">
              <a-input v-model="model.itemModel" placeholder="请输入型号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="品牌" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="brand">
              <a-input v-model="model.brand" placeholder="请输入品牌"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="二级供应商" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="secondarySupplier">
              <a-input v-model="model.secondarySupplier" placeholder="请输入二级供应商"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'MaterialDetailTableForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        isDisabled: true,
        model:{
          supplierCode:'1012867',
          supplierName:'Shenzhen Changhong Technology Co.,Ltd.',
          origin:'China'
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           materialNumber: [
              { required: true, message: '请输入料号!'},
           ],
           quantity: [
              { required: true, message: '请输入数量!'},
           ],
           productionDate: [
              { required: true, message: '请输入生产日期!'},
              { max: 8, message: '长度不超过8!'},
              { min: 8, message: '长度不小于8!'},   
              //{ type: 'number',message: '必须填入数字！'}
              { pattern: new RegExp(/^[1-9]\d*$/, "g"),message: '请遵循示例格式！'}

           ],
           supplierCode: [
              { required: true, message: '请输入供应商代码!'},
           ],
           supplierName: [
              { required: true, message: '请输入供应商名称!'},
           ],
        },
        url: {
          add: "/ch/materialDetailTable/add",
          edit: "/ch/materialDetailTable/edit",
          queryById: "/ch/materialDetailTable/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      console.log('test'+Math.random().toString(36).substr().substring(3,13).toUpperCase())
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>